package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.UpdatePwdContract;
import com.yiwei.zhh.api.requestInfo.UpdatePwdRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.MD5;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;
import com.yiwei.zhh.view.CircularProgress;

/**
 * 修改密码界面
 * Created by Master.Dong on 16/4/10.
 */
public class UpdatePwdActivity extends BaseActivity implements View.OnClickListener, UpdatePwdContract.View {

    private CircularProgress loading;
    private EditText oldPwd;
    private CheckBox mSeeBtn;
    private TextView nextBtn;
    private UpdatePwdRequest api;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.update_pwd_activity);
        baseInitView(this, "UpdatePwdActivity", Const.ViewType.ACTIVITY);
        initView();

    }

    private void initView() {
        api = UpdatePwdRequest.create();
        api.attachView(this);
        setTitleText(getResources().getString(R.string.update_password));
        setBackIntent();

        loading = (CircularProgress) findViewById(R.id.progressBar);
        oldPwd = (EditText) findViewById(R.id.old_pwd);
        mSeeBtn = (CheckBox) findViewById(R.id.see);
        nextBtn = (TextView) findViewById(R.id.next);
        nextBtn.setOnClickListener(this);
        ((TextView) findViewById(R.id.forget_pwd)).setOnClickListener(this);

        mSeeBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mSeeBtn.setBackgroundResource(R.mipmap.see_off);
                    oldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //显示密码
                } else {
                    mSeeBtn.setBackgroundResource(R.mipmap.see);
                    oldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); //隐藏密码
                }
                oldPwd.setSelection(oldPwd.getText().length());
            }
        });
    }

    /**
     * 检查输入是否为空
     */
    private boolean checkInput() {
        if (TextUtils.isEmpty(oldPwd.getText().toString().trim())) {
            ToastUtil.makeText(this, getResources().getString(R.string.password_is_empty), Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (loading != null) {
            if (b) {
                nextBtn.setEnabled(false);
                loading.setVisibility(View.VISIBLE);
            } else {
                nextBtn.setEnabled(true);
                loading.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next:
                if (checkInput())
                    getCheckOldPwd();
                break;
            case R.id.forget_pwd:
                Intent intent = new Intent();
                intent.putExtra("type", Const.ForgetPwdType.CHECK_PHONE);
                intent.putExtra("showLogo", false);
                intent.setClass(UpdatePwdActivity.this, ForgetPwdActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                UpdatePwdActivity.this.finish();
                break;
        }
    }

    /**
     * 获取检查密码信息
     */
    private void getCheckOldPwd() {
        api.getCheckPwdInfo(MD5.getMD5str(oldPwd.getText().toString().trim()));
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getCheckPwdInfoSuccess(ResponseModel item) {
        Intent intent = new Intent(this, ForgetPwdActivity.class);
        intent.putExtra("update", true);
        startActivity(intent);
        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
    }
}
